package com.game.majiang.vo;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import lombok.Data;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.SqlHelperwrite;
import com.game.server.db.entity.Proxy;
import com.game.server.util.Util;
@Data
public class ProxyVo {
	//id,,,uid,,
	
	int id;
	double integral;
	Date createTime;
	int uid;
	int isqianfei;
	Date qishishijian;

	//查玩家代理 获得id url
	public static int checkproxy1(int uid){
		int i=0;
		String cmdText = "SELECT id,erweimaUrl FROM proxy WHERE uid =?";
		Parameter p1 = new Parameter(Types.INTEGER, uid,ParameterType.Input);
		CachedRowSet rs =SqlHelper.getResultSet(cmdText, p1);
		//String[] str= new String[2];
		try {
			while (rs.next()) {
//				str[0] = rs.getString(1);
//
//				str[1] = rs.getString(2);
				i=1;

			}
			if(i!=1) {
				String cmdText1 = "SELECT id FROM proxytoexamine_log WHERE uid = ? AND state<>2";
				Parameter p11 = new Parameter(Types.INTEGER,uid,ParameterType.Input);
				CachedRowSet rs2 =SqlHelper.getResultSet(cmdText1, p11);
				try {
					while (rs2.next()) {
						i=2;

					}
			
				
				} catch (SQLException e) {
			
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
//		if("".equals(str[0])||str[0]==null){
//			str[0] = "0";
//		}
		return i;

	}
	//查玩家代理 获得id url
		public static String[] checkproxy(int uid){

			String cmdText = "SELECT id,erweimaUrl FROM proxy WHERE uid =?";
			Parameter p1 = new Parameter(Types.INTEGER, uid,ParameterType.Input);
			CachedRowSet rs =SqlHelper.getResultSet(cmdText, p1);
			String[] str= new String[2];
			try {
				while (rs.next()) {
					str[0] = rs.getString(1);
	
					str[1] = rs.getString(2);
				

				}
			
			} catch (SQLException e) {
			
				e.printStackTrace();
			}
			if("".equals(str[0])||str[0]==null){
				str[0] = "0";
			}
			return str;

		}
	//检查茶楼名称
	public static boolean checkteaname(String name){

		String cmdText = "SELECT id FROM proxy WHERE nickname = ?;";
		Parameter p1 = new Parameter(Types.VARCHAR, name,ParameterType.Input);
		Object obj=SqlHelper.execScalar(cmdText, p1);
		if(obj!=null){
			return (int)obj>0;
		}
		return false;

	}

	//用户pid
	public static int checkUserPid(int uid){

		String cmdText = "SELECT pid FROM users WHERE id = ?;";
		Parameter p1 = new Parameter(Types.INTEGER, uid,ParameterType.Input);
		Object obj=SqlHelper.execScalar(cmdText, p1);
		if(obj!=null){
			return (int)obj;
		}
		return 0;

	}
	//用户id
		public static int checkProxyuid(int id){

			String cmdText = "SELECT uid FROM proxy WHERE id = ?;";
			Parameter p1 = new Parameter(Types.INTEGER, id,ParameterType.Input);
			Object obj=SqlHelper.execScalar(cmdText, p1);
			if(obj!=null){
				return (int)obj;
			}
			return 0;

		}
	//添加代理
	public static void insertProxy(LogicClient client,String WeChat,String truename,String phone,String chalouname){
		String cmdText = "INSERT INTO proxy (account,password,pid,nickname,contactWay,createTime,trueName,phone,uid,head,qishishijian,isqianfei) "
				+ "VALUES (?,?,?,?,?,now(),?,?,?,?,now(),1)";
		Parameter p1 = new Parameter(Types.VARCHAR,phone,ParameterType.Input);
		Parameter p2 = new Parameter(Types.VARCHAR, Util.Md5("123456"),ParameterType.Input);
		Parameter p3 = new Parameter(Types.INTEGER, checkUserPid(client.getUserId()),ParameterType.Input);
		Parameter p4 = new Parameter(Types.VARCHAR, chalouname,ParameterType.Input);
		Parameter p5 = new Parameter(Types.VARCHAR, WeChat,ParameterType.Input);
		Parameter p6 = new Parameter(Types.VARCHAR, truename,ParameterType.Input);
		Parameter p7 = new Parameter(Types.VARCHAR, phone,ParameterType.Input);
		Parameter p8 = new Parameter(Types.INTEGER, client.getUserId(),ParameterType.Input);
		Parameter p9 = new Parameter(Types.VARCHAR, client.getVo().getHead(),ParameterType.Input);
		SqlHelperwrite.execute(cmdText, p1,p2,p3,p4,p5,p6,p7,p8,p9);
//		updateProxyerweimaUrl(client.getUserId());
		UserVo.addtuijian(checkProxyid(client.getUserId()), client.getUserId());
	}
	//查代理id
	public static int checkProxyid(int uid){

		String cmdText = "SELECT id FROM proxy WHERE uid = ? ";
		Parameter p1 = new Parameter(Types.INTEGER, uid,ParameterType.Input);
		Object obj=SqlHelper.execScalar(cmdText, p1);
		if(obj!=null){
			return (int)obj;
		}
		return 0;
	}
//	//修改代理二维码
//	public static void updateProxyerweimaUrl(int userid)
//	{
//		
//		String url = DataManager.getInstance().systemValue("bangproxy")+checkProxyid(userid);
//		String cmdText = "update proxy set erweimaUrl = ?  where uid = ? ";
//		Parameter p1 = new Parameter(Types.VARCHAR, url, ParameterType.Input);	
//		Parameter p2 = new Parameter(Types.INTEGER, userid, ParameterType.Input);	
//		SqlHelper.execute(cmdText, p1,p2);
//	}
	/**
	 * 小目标进度
	 * */
	public static  double sqlAddSum(int id, String begin, String end) {
		double count= 0;
		String cmdText= "select cast(IFNULL(SUM(fl.money),0) as decimal(18,2)) from (select uid,oneproxyid,(CASE WHEN oneproxyid="+id+" THEN oneproxypump WHEN twoproxyid="+id+" THEN twoproxypump WHEN threeproxyid="+id+" THEN threeproxypump END) AS money,if((select id FROM proxy where uid =p.uid ),'1','2') AS isVip,(CASE WHEN oneproxyid="+id+" THEN '1' WHEN twoproxyid="+id+" THEN '2' WHEN threeproxyid="+id+" THEN '3' END) AS level from pump_log p where (oneproxyid = "+id+"  or oneproxyid in (select id from proxy where pid = "+id+") or oneproxyid IN (select id from proxy where pid in (select id FROM proxy where pid ="+id+"))) and createtime>='"+begin+"' and createtime<='"+end+"') AS fl";
		Object obj=SqlHelper.execScalar(cmdText);
		if(obj!=null){
			return Double.parseDouble(obj.toString());
		}
        return count;
	}
	
	
	/**
	 * 
	 * 获取全部代理
	 * */
	public static List<ProxyVo> getProxy(){
		List<ProxyVo> pp= new ArrayList<ProxyVo>();
		String cmdText = "SELECT id,integral,createTime,uid,isqianfei,qishishijian FROM proxy where `status`=1";
	
		CachedRowSet rs =SqlHelper.getResultSet(cmdText);
		try {
			while (rs.next()) {
				ProxyVo proxyVo = new ProxyVo();
				proxyVo.setId( rs.getInt("id"));
				proxyVo.setIntegral( rs.getDouble("integral"));
				proxyVo.setIsqianfei(rs.getInt("isqianfei"));
				proxyVo.setUid(rs.getInt("uid"));
				proxyVo.setCreateTime(rs.getDate("createTime"));
				proxyVo.setQishishijian(rs.getDate("qishishijian"));
				pp.add(proxyVo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return pp;

	}
	public static void updateProxy(int id2,String ziduan,int value) {
		String cmdText = "update proxy set "+ziduan+"= ?  where id = ? ";
		Parameter p1 = new Parameter(Types.INTEGER, value, ParameterType.Input);	
		Parameter p2 = new Parameter(Types.INTEGER, id2, ParameterType.Input);	
		SqlHelperwrite.execute(cmdText, p1,p2);
		
	}
	public static void updateProxy2(int id2) {
		String cmdText = "update proxy set qishishijian = now() where id = ? ";
		Parameter p1 = new Parameter(Types.INTEGER, id2, ParameterType.Input);	
		SqlHelperwrite.execute(cmdText,p1);
		
	}
	
	/**
	 * 
	 * 查询推广员是否欠费
	 * */
	public static int getProxyqianfei(int id){
		int qianfei=1;
		String cmdText = "SELECT isqianfei FROM proxy where uid=?";
		Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);	
		CachedRowSet rs =SqlHelper.getResultSet(cmdText,p1);
		try {
			while (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return qianfei;

	}
	
	
	/**
	 * 
	 * 查询推广员身份是否被封禁
	 * */
	public static int getProxyFengJin(int id){
		int status=1;
		String cmdText = "SELECT status FROM proxy where uid=?";
		Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);	
		CachedRowSet rs =SqlHelper.getResultSet(cmdText,p1);
		try {
			while (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return status;

	}
	public static void updateProxy3(int userId) {
		
		String cmdText = "update proxy set isqianfei = 1 where uid = ? ";
		Parameter p1 = new Parameter(Types.INTEGER, userId, ParameterType.Input);	
		SqlHelperwrite.execute(cmdText,p1);
	}
	public static Proxy selectUserProxy(int uid) {
		String sql = "SELECT * FROM proxy WHERE uid = ?";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);	
		CachedRowSet resultSet = SqlHelper.getResultSet(sql, p1);
		try {
			if (resultSet.next()) {
				Proxy p = new Proxy();
				p.account = resultSet.getString("account");
				p.id = resultSet.getInt("id");
				p.password = resultSet.getString("password");
				p.pid = resultSet.getInt("pid");
				p.integral = resultSet.getDouble("integral");
				p.nickname = resultSet.getString("nickname");
				p.contactWay = resultSet.getString("contactWay");
				p.proxyType = resultSet.getInt("proxyType");
				p.status = resultSet.getInt("status");
				p.ratio = resultSet.getInt("ratio");
				p.logintime = resultSet.getString("logintime");
				p.createTime = resultSet.getString("createTime");
				p.trueName = resultSet.getString("trueName");
				p.phone = resultSet.getString("phone");
				p.uid = resultSet.getInt("uid");
				p.erweimaUrl = resultSet.getString("erweimaUrl");
				p.promotionmapUrl = resultSet.getString("promotionmapUrl");
				p.bindTime = resultSet.getString("bindTime");
				p.head = resultSet.getString("head");
				p.yiji = resultSet.getInt("yiji");
				p.erji = resultSet.getInt("erji");
				p.sanji = resultSet.getInt("sanji");
				p.right = resultSet.getInt("right");
				p.isqianfei = resultSet.getInt("isqianfei");
				p.qishishijian = resultSet.getString("qishishijian");
				p.sumGolde = resultSet.getString("sumGolde");
				p.allhongli = resultSet.getInt("allhongli");
				p.yuhongli = resultSet.getInt("yuhongli");
				p.tiqu = resultSet.getInt("tiqu");
				return p;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//今日新增
	public static int toDayNewlyAdded(int pid) {
		String sql = "SELECT COUNT(id) FROM users WHERE pid = ? AND TO_DAYS(createtime) = TO_DAYS(NOW())";
		Parameter p1 = new Parameter(Types.BIGINT, pid, ParameterType.Input);
		Object execScalar = SqlHelper.execScalar(sql, p1);
		if (execScalar == null) {
			return 0;
		}
		Long l = (Long)execScalar;
		return l.intValue();
	}
	
	//直属玩家信息
	public static void directPlayer(int pid) {
		String sql = "SELECT u1.id,u1.head,u1.nickname,u1.zongshoushu,p1.id AS ppid FROM (SELECT id,head,nickname,zongshoushu FROM users  WHERE pid = ?) u1 LEFT JOIN proxy p1 ON p1.uid = u1.id";
		Parameter p1 = new Parameter(Types.BIGINT, pid, ParameterType.Input);
		CachedRowSet resultSet = SqlHelper.getResultSet(sql, p1);
		try {
			while (resultSet.next()) {
				
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
